#!/bin/bash

# RK Vendor Storage Boot Service Installation Script
# Author: Zhang Zhen <zhangzhen@cmhi.chinamobile.com>

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration
BINARY_NAME="YamlSN"
SERVICE_NAME="rk-vendor-reader.service"
INSTALL_DIR="/usr/local/bin"
SERVICE_DIR="/etc/systemd/system"
OUTPUT_DIR="/opt/zyhy/params"

echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}  RK Vendor Storage Boot Service Installer     ${NC}"
echo -e "${BLUE}  Author: Zhang Zhen                           ${NC}"
echo -e "${BLUE}================================================${NC}"
echo

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   echo -e "${RED}错误：此脚本需要root权限运行${NC}"
   echo "请使用: sudo $0"
   exit 1
fi

# Check if binary exists
if [ ! -f "./bin/$BINARY_NAME" ]; then
    echo -e "${RED}错误：找不到 bin/$BINARY_NAME 程序${NC}"
    echo "请先运行 'make all' 编译程序"
    exit 1
fi

# Check if service file exists
if [ ! -f "./service/$SERVICE_NAME" ]; then
    echo -e "${RED}错误：找不到 service/$SERVICE_NAME 服务文件${NC}"
    exit 1
fi

echo -e "${YELLOW}正在安装RK Vendor Storage开机读取服务...${NC}"
echo

# Step 1: Create output directory
echo -e "${BLUE}步骤 1: 创建输出目录${NC}"
if [ ! -d "$OUTPUT_DIR" ]; then
    mkdir -p "$OUTPUT_DIR"
    echo -e "${GREEN}✓ 创建目录: $OUTPUT_DIR${NC}"
else
    echo -e "${GREEN}✓ 目录已存在: $OUTPUT_DIR${NC}"
fi

# Step 2: Copy binary to system directory
echo -e "${BLUE}步骤 2: 安装程序到系统目录${NC}"
cp "./bin/$BINARY_NAME" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
echo -e "${GREEN}✓ 安装程序: $INSTALL_DIR/$BINARY_NAME${NC}"

# Step 3: Install systemd service
echo -e "${BLUE}步骤 3: 安装systemd服务${NC}"
cp "./service/$SERVICE_NAME" "$SERVICE_DIR/"
echo -e "${GREEN}✓ 安装服务文件: $SERVICE_DIR/$SERVICE_NAME${NC}"

# Step 4: Reload systemd and enable service
echo -e "${BLUE}步骤 4: 配置systemd服务${NC}"
systemctl daemon-reload
echo -e "${GREEN}✓ 重新加载systemd配置${NC}"

systemctl enable "$SERVICE_NAME"
echo -e "${GREEN}✓ 启用开机自启动服务${NC}"

# Step 5: Test the service
echo -e "${BLUE}步骤 5: 测试服务${NC}"
echo -e "${YELLOW}正在启动服务进行测试...${NC}"

# Remove immutable attribute from output file if it exists
if [ -f "$OUTPUT_DIR/device_config.yaml" ]; then
    echo -e "${BLUE}解除配置文件保护属性...${NC}"
    chattr -i "$OUTPUT_DIR/device_config.yaml" 2>/dev/null || true
    echo -e "${GREEN}✓ 已解除文件保护${NC}"
fi

if systemctl start "$SERVICE_NAME"; then
    echo -e "${GREEN}✓ 服务启动成功${NC}"
    
    # Wait a moment for the service to complete
    sleep 2
    
    # Check service status
    if systemctl is-active --quiet "$SERVICE_NAME"; then
        echo -e "${GREEN}✓ 服务运行正常${NC}"
    fi
    
    # Check if output file was created
    if [ -f "$OUTPUT_DIR/device_config.yaml" ]; then
        echo -e "${GREEN}✓ 配置文件生成成功${NC}"
        
        # Set immutable attribute to protect the file
        echo -e "${BLUE}设置配置文件保护属性...${NC}"
        chattr +i "$OUTPUT_DIR/device_config.yaml" 2>/dev/null || true
        echo -e "${GREEN}✓ 已设置文件保护${NC}"
        
        echo -e "${BLUE}生成的配置文件内容:${NC}"
        echo "----------------------------------------"
        cat "$OUTPUT_DIR/device_config.yaml"
        echo "----------------------------------------"
    else
        echo -e "${YELLOW}⚠ 配置文件未生成，可能需要检查vendor storage设备${NC}"
    fi
else
    echo -e "${RED}✗ 服务启动失败${NC}"
    echo -e "${YELLOW}查看服务状态: systemctl status $SERVICE_NAME${NC}"
    echo -e "${YELLOW}查看服务日志: journalctl -u $SERVICE_NAME${NC}"
fi

echo
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}  安装完成！                                   ${NC}"
echo -e "${GREEN}================================================${NC}"
echo
echo -e "${BLUE}服务管理命令:${NC}"
echo "  启动服务: systemctl start $SERVICE_NAME"
echo "  停止服务: systemctl stop $SERVICE_NAME"
echo "  查看状态: systemctl status $SERVICE_NAME"
echo "  查看日志: journalctl -u $SERVICE_NAME"
echo "  禁用服务: systemctl disable $SERVICE_NAME"
echo
echo -e "${BLUE}配置文件位置:${NC}"
echo "  $OUTPUT_DIR/device_config.yaml"
echo
echo -e "${YELLOW}注意：服务将在每次开机时自动运行，从vendor storage读取配置并更新YAML文件${NC}"
